const express = require('express');
const fetch = require('node-fetch');

const app = express();
const PORT = 3000;

app.use(express.json());

app.get('/proxy', async (req, res) => {
    try {
        const { query, conversation_id, user } = req.query;
        const url = `http://8.220.140.148/v1/chat-messages?query=${encodeURIComponent(query)}&response_mode=streaming&conversation_id=${conversation_id}&user=${user}`;

        const response = await fetch(url);
        const data = await response.text();

        res.setHeader('Access-Control-Allow-Origin', '*');
        res.send(data);
    } catch (error) {
        res.status(500).send('Error occurred while processing request');
    }
});

app.listen(PORT, () => {
    console.log(`Proxy server running on port ${PORT}`);
});